//
//  THJProvinceCityVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/5/8.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJProvinceCityVC.h"

@interface THJProvinceCityVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * _tableview;
    
    NSInteger clickType;
    
    NSString * provinceStr;
}
@property(strong,nonatomic)NSArray * dataArray;
@end

@implementation THJProvinceCityVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = SingleColor(whiteColor);
    [self getProvinceList];
    [self setTableview];
    clickType = 1;
    // Do any additional setup after loading the view.
}
-(void)getProvinceList{
     [MBProgressHUD showMessage:@"正在加载..." toView:self.view];
    NSString * urlstr = [NSString stringWithFormat:@"%@store.php?action=getarealist&parentid=0&level=1&token=%@",THJDomain,[THJTools getToken]];
    __weak typeof(self) weakself= self;
    [THJNetWork getCompanyIntroFromUrl:urlstr callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"省市--%@",receiveData);
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        weakself.dataArray = receiveData[@"data"];
        [_tableview reloadData];
    } Type:2];
}
-(void)setTableview{
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableview];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    NSDictionary * dic = self.dataArray[indexPath.row];
    cell.textLabel.text = dic[@"text"];
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = self.dataArray[indexPath.row];
    NSLog(@"选择的省--%@",dic[@"value"]);
    if(clickType == 2){
        NSString * provinceCity = [NSString stringWithFormat:@"%@-%@",provinceStr,dic[@"text"]];
        if([self.delegate respondsToSelector:@selector(chooseItem:ItemValue:)]){
            [self.delegate chooseItem:provinceCity ItemValue:dic[@"value"]];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
    if (clickType == 1) {
        provinceStr = dic[@"text"];
        NSString * value = dic[@"text"];
        if([value isEqual:@"北京"]||[value isEqual:@"天津"]||[value isEqual:@"上海"]||[value isEqual:@"重庆"]||[value isEqual:@"澳门"]||[value isEqual:@"香港"]||[value isEqual:@"台湾"]){
            if([self.delegate respondsToSelector:@selector(chooseItem:ItemValue:)]){
                [self.delegate chooseItem:dic[@"text"] ItemValue:dic[@"value"]];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [MBProgressHUD showMessage:@"正在加载..." toView:self.view];
            NSString * urlstr = [NSString stringWithFormat:@"%@store.php?action=getarealist&parentid=%@&level=2&token=%@",THJDomain,dic[@"value"],[THJTools getToken]];
            __weak typeof(self) weakself= self;
            [THJNetWork getCompanyIntroFromUrl:urlstr callback:^(NSDictionary* receiveData, NSError *error) {
                NSLog(@"市--%@",receiveData);
                [MBProgressHUD hideHUDForView:weakself.view animated:YES];
                weakself.dataArray = receiveData[@"data"];
                [_tableview reloadData];
            }Type:2];
        }
        clickType = 2;
        }
}
@end
